#Script to verify the current code base compliance
# with kernels coding standards.
# Run it with the arguments as your top level directory
# of the project.

SRC=$@
checkpatch=`which checkpatch.pl`
[ -z "$checkpatch" ] && checkpatch=`dirname $0`/checkpatch.pl

[ "$SRC" = "" ] && exit 1

FILE=/tmp/1.c

echo "                                      errors  warnings  lines of code   errors/LOC"

for DIR in $SRC; do

    for file in ` find $DIR -name '*.[ch]'`
    do 
	cat $file > $FILE
	RES=`$checkpatch --no-tree --file $FILE | grep ^total`

  # echo $RES

	if [ "$RES" != "" ]; then
	    ERR=`echo $RES | cut -d' ' -f2`
	    WARN=`echo $RES | cut -d' ' -f4`
	    LINES=`echo $RES | cut -d' ' -f6`
	else
	    ERR=0
	    WARN=0
	    LINES=`cat $FILE | wc -l`
	fi

	LINES=`expr $LINES + 1`
	rm -f $FILE
	ERR_LOC=`( echo "scale=1"; echo "$ERR*100/$LINES" ) | bc`

	printf "%-35s " $DIR/$file
	printf "%8s      %4s    %10s    %10s%%\n" $ERR $WARN $LINES $ERR_LOC
    done

done
